<?php
/**
*+-------qq账号接入控制器--------+
*+-------------------------------------------------+
*-------对于qq用户接入的控制处理
*+-------------------------------------------------+
*+-@author susy.liang <liangling@ilongyuan.com.cn>-+
*+-------------------------------------------------+
*/
namespace Auth\Event;
use Auth\Event\PublicEvent;

class QqEvent extends PublicEvent {

	/**
	*成员变量的初始化
	*/
	protected function _initialize(){

		$this->params 	= 	C('thirdParty.qq');
		$this->appId 	= 	$this->params['appId'];
		$this->appKey 	= 	$this->params['appKey'];
	}
	
	/**
	*qq的回调
	*/
	 public function callBack($param=array()){

	 	if($param['state'] && $param['code']){//code的回调
   			$state = session('state');
   			if($state != $_GET['state']){
   				echo 'param:state illegal';
   				exit();
   			}
   			
   			$this->getToken($param['code'], U('Auth/callBack', array('channel'=>'qq')));

   		}elseif($param['access_token']){//token的回调
   			$token = $param['access_token'];
   			$openid = $this->getOpenid($token);
   			if(!$openid){
   				echo 'get openid failed';
   				exit();
   			}
   			$user = $this->getUser($openid, $token);
   			//TODO:add user
   			exit();
   		}
	 }

	/**
	*请求授权 authorize code
	*@param string $redirectUrl 回调的地址
	*@return boolean
	*/
	public function getCode($redirectUrl=''){
		if(empty($redirectUrl))
			return false;
		$redirectUrl = urlencode($redirectUrl);

		$state = md5(uniqid(time(), true));
		session('state', $state);

		$requestParam = array(
						'response_type'=>'code',
						'client_id'=>$this->appId,
						'redirect_uri'=>$redirectUrl,
						'state'=>$state,
						'scope'=>$this->params['scope']
			);
		$this->doHttp($this->params['codeApi'], $requestParam);

		return true;
	}

	/**
	*请求授权令牌 token
	*@param string $authCode 授权码
	*@param string $state 授权时返回的服务器设置的状态码
	*@param string $redirectUrl 回调地址
	*@return boolean true|false
	*/
	public function getToken($authCode='', $redirectUrl){
		if(empty($authCode) || empty($redirectUrl))
			return false;

		$requestParam = array(
						'grant_type'=>'authorization_code',
						'client_id'=>$this->appId,
						'client_secret'=>$this->appKey,
						'code'=>$authCode,
						'redirect_uri'=>$redirectUrl
			);
		$this->doHttp($this->params['tokenApi'], $requestParam);

		return true;
	}

	/**
	*获取qq用户的身份标识openid
	*@param string $token 请求的授权令牌
	*/
	public function getOpenid($token=''){
		if(empty($token))
			return false;

		$requestParam = array(
						'access_token'=>$token
			);
		$res = $this->doHttp($this->params['openidApi'], $requestParam);
		if(!$res['callback']['openid'])
			return false;
		else
			return $res['callback']['openid'];
	}

	/**
	*根据token获取用户基本信息
	*如昵称
	*/
	public function getUser($openid='', $token=''){
		if(empty($openid))
			return false;

		$requestParam = array(
						'access_token'=>$token,
						'oauth_consumer_key'=>$this->appId,
						'openid'=>$openid,
			);
		$res = $this->doHttp($this->params['userApi'], $requestParam);
		if($res['nickname']){
			return $res['nickname'];
		}else{
			return false;
		}
	}

}